home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / CW MacMindy 1.4 / Examples / Modules / stack.dyl < prev   
Encoding:
Text File  |  1995-11-14  |  767 b   |  35 lines  |  [TEXT/CWIE]

  1. module:            Stack
  2. author:            Patrick C. Beard <beard@cs.ucdavis.edu>
  3. description:    A simple LIFO stack.
  4.  
  5. define module Stack
  6.     use Dylan;                // all programs need this.
  7.     export
  8.         <stack>,
  9.         top;
  10. end module Stack;
  11.  
  12. define class <stack> (<object>)
  13.     slot %contents :: <list>,
  14.         init-value: #();
  15. end class <stack>;
  16.  
  17. define method push (stack :: <stack>, value) => (stack :: <stack>);
  18.     stack.%contents := pair(value, stack.%contents);
  19.     stack;
  20. end method;
  21.  
  22. define method pop (stack :: <stack>) => (value :: <object>);
  23.     let value = top(stack);
  24.     stack.%contents := tail(stack.%contents);
  25.     value;
  26. end method;
  27.  
  28. define method top (stack :: <stack>) => (value :: <object>);
  29.     if (~empty?(stack.%contents))
  30.         head(stack.%contents);
  31.     else
  32.         error("stack is empty!");
  33.     end if;
  34. end method;
  35.